package cc.inik.goodsquery;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends AppCompatActivity {

    private EditText editText;
    private TextView product_name;
    private TextView product_price;
    private String epc;
    private RFIDInit rfidInit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        this.bindView();
        rfidInit = new RFIDInit(2,115200);

    }

    private void bindView(){
        this.editText = findViewById(R.id.input_code);
        this.product_name = findViewById(R.id.product_name);
        this.product_price = findViewById(R.id.product_price);
    }

    public void getEpcBtn(View view) throws Exception {
        rfidInit.getEpc(val->this.editText.setText(val));

        rfidInit.readData(str->this.product_price.setText(str));

    }

    public void writeEpcBtn(View view) {
        Intent intent = new Intent(MainActivity.this,MainActivity2.class);
        Bundle bundle = new Bundle();
        epc = editText.getText().toString();
        bundle.putString("epc",epc);
        intent.putExtras(bundle);
        startActivityForResult(intent,0x01);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == 0x01 && resultCode == 0x01){
            Bundle bundle = data.getExtras();
            String product_name = bundle.getString("product_name");
            String product_price = bundle.getString("product_price");
            Log.i("pn",product_name);
            Log.i("pp",product_price);
            try {
                rfidInit.writeData(product_name,isSuccess->Toast.makeText(MainActivity.this,"写入商品名成功",Toast.LENGTH_SHORT).show());
                rfidInit.writeData(product_price,isSuccess->Toast.makeText(MainActivity.this,"写入商品价格成功",Toast.LENGTH_SHORT).show());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}